$(document).ready(function(){
	var ctx = $("#ctx").val();//获取服务器地址
	
	//初始页面加载权限
	accountid = $('#accountId').val();
	url = ctx + "/back/main/account/getNowResources?accountId="+accountid;
	$.ajax({
		async: false, 
		traditional: true, 
        type: "POST",
         url: url,
         data:{"id":accountid},
         success: function(data){
        	nowresource = data; 
        	//alert("nowresource"+nowresource);
        	for(i=0;i<nowresource.length;i++){
       		 checked = $(".show_check");
       		 $(".show_check").each(function(){
       			a = $(this).val();
       			//alert("a="+a);
       			//alert("nowresource[i].resourceId"+nowresource[i].resourceId);
       			if( a==nowresource[i].id){
          			 $(this).attr("checked", true);
       			}
       		 })
        	}
         }
	});
	
//点击角色更改对应的权限
	$("input[name='roleId']").click(function(){
		roleid = $(this).attr("id");
		url = ctx + "/back/main/account/getRoleResource?roleId="+roleid;
		$.ajax({
			async: false, 
			traditional: true, 
	        type: "POST",
	         url: url,
	         data:{"id":roleid},
	         success: function(data){
	        	 $(".show_check").each(function(){
		       			$(this).attr("checked", false);
	        	 });
	        	nowresource = data; 
	        	for(i=0;i<nowresource.length;i++){
	       		 checked = $(".show_check");
	       		 $(".show_check").each(function(){
	       			a = $(this).val();
	       			if( a==nowresource[i]){
	          			 $(this).attr("checked", true);
	       			}
	       		 })
	        	}
	         }
		});
	});
	//复选框子类和父类对应选择
	$(".show_check").click(function(){
		thisid = $(this).attr("id");
		judge = thisid.split("_");
		parentidclass = ".self"+judge[2];
		if(judge[0]=="root"){
			classjudgeid = ".root"+judge[1];	
		}else if(judge[0]=="menu"){
			classjudgeid = ".menu"+judge[1];
		}else{
			classjudgeid = ".button"+judge[1];
		}
		if_checked = $(this).is(':checked');
		$(classjudgeid).attr("checked", if_checked);
		if(!if_checked){
			$(parentidclass).attr("checked", if_checked);
		}
	})
});
